<?php

namespace app\admin\controller\financing;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 补缴欠款日志
 *
 * @icon fa fa-user
 */
class Debt extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Debt;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $id = request()->param()['order_id'];
            $list = $this->model
                //->with(["users"=>['id','name'],"medic"=>['id','name'],"info"=>['id','name']])
                ->with([
                    'accepts' => function ($query) {
                        $query->withField('id,code');
                    },
                    'admin' => function ($query) {
                        $query->withField('id,nickname');
                    },
                ])
                //->where($where)
                ->where('order_id',$id)
                ->order('id', 'desc')
                ->paginate($limit);

            if (!empty($list->items())){
                foreach ($list->items() as $k=>$v){
                    //1代表转账，2代表现金，3代表pos机，4代表支付宝，5代表微信，6代表储值卡，7代表积分
                    switch ($v->pays){
                        case 1:
                            $list->items()[$k]->pays = '转账';
                            break;
                        case 2:
                            $list->items()[$k]->pays = '现金';
                            break;
                        case 3:
                            $list->items()[$k]->pays = 'pos机';
                            break;
                        case 4:
                            $list->items()[$k]->pays = '支付宝';
                            break;
                        case 5:
                            $list->items()[$k]->pays = '微信';
                            break;
                        case 6:
                            $list->items()[$k]->pays = '储值卡';
                            break;
                        case 7:
                            $list->items()[$k]->pays = '积分';
                            break;
                        default:
                            $list->items()[$k]->pays = '无';
                            break;
                    }

                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}
